// tge.rs
// copyright zipxing@hotmail.com 2022~2023

//! log提供日志接口实现
//! 参考https://docs.rs/log4rs

use crate::util::get_abs_path;
use log::LevelFilter;
use log4rs::{
    append::file::FileAppender,
    config::{Appender, Config, Root},
    encode::pattern::PatternEncoder,
    filter::threshold::ThresholdFilter,
};

/// 初始化日志系统
pub fn init_log(level: LevelFilter, file_path: &str) {
    let fpstr = get_abs_path(file_path);
    let logfile = FileAppender::builder()
        .encoder(Box::new(PatternEncoder::new(
            "{d(%Y-%m-%d %H:%M:%S)} {l} {t} {m}{n}\n",
        )))
        .build(fpstr)
        .unwrap();
    let config = Config::builder()
        .appender(
            Appender::builder()
                .filter(Box::new(ThresholdFilter::new(level)))
                .build("logfile", Box::new(logfile)),
        )
        .build(
            Root::builder()
                .appender("logfile")
                .build(LevelFilter::Trace),
        )
        .unwrap();
    let _handle = log4rs::init_config(config).unwrap();
}
